using UnityEngine;

public class ZombieBehaviour : EntityBehaviour
{
	public Vector2 distPlayer = new Vector2(100f, 100f);

	public Vector2 distBarricade = new Vector2(100f, 100f);

	public bool strikeOnRun = true;

	public int strikeCount;

	private float timeMarkStrikePlayer;

	public ZombieBehaviour(ZombieInHome zombie)
		: base(zombie)
	{
	}

	public override void OnUpdate()
	{
		if (zombie.isDead)
		{
			return;
		}
		stateAnimator = zombie.animator.GetCurrentAnimatorStateInfo(0);
		if (zombie.speedMove != 0f)
		{
			currentSpeed.x = zombie.speedMove * stateAnimator.speed * stateAnimator.speedMultiplier * Mathf.Min(0.05f, Time.deltaTime);
			currentSpeed.y = 0f;
			if (Player.onStage != null && ZombieBarricade.onStage == null)
			{
				distPlayer = Player.onStage.transform.position - zombie.transform.position;
				if (Mathf.Abs(distPlayer.x) < 12f && currentSpeed.x < 0f && Mathf.Abs(distPlayer.y) > 0.2f)
				{
					currentSpeed.y = Mathf.Sin(Mathf.Atan2(distPlayer.y, distPlayer.x)) * Mathf.Abs(currentSpeed.x);
				}
			}
			zombie.transform.position += currentSpeed;
		}
		if (ZombieBarricade.onStage != null)
		{
			distBarricade = zombie.transform.position - ZombieBarricade.onStage.transform.position;
			distBarricade.x = Mathf.Floor(distBarricade.x * 100f) / 100f;
			if (distBarricade.x < 0f)
			{
				zombie.transform.position = ZombieBarricade.onStage.transform.position;
				zombie.transform.Translate(0f, distBarricade.y, 0f);
				distBarricade.x = 0f;
			}
			if (distBarricade.x == 0f && (stateAnimator.IsTag("stand") || stateAnimator.IsTag("move") || stateAnimator.normalizedTime >= 1f))
			{
				zombie.animator.Play("Удар", 0, 0f);
			}
		}
		else
		{
			if (!(Player.onStage != null) || Player.onStage.isDead)
			{
				return;
			}
			distPlayer = Player.onStage.transform.position - zombie.transform.position;
			distPlayer.x = Mathf.Abs(distPlayer.x);
			distPlayer.x = Mathf.Floor(distPlayer.x * 100f) / 100f;
			if (distPlayer.x < 3f && stateAnimator.IsName("Бег") && strikeOnRun)
			{
				zombie.animator.Play("УдарРазбег", 0, 0f);
				strikeCount++;
				strikeOnRun = false;
			}
			else if (distPlayer.x < 2.5f && stateAnimator.IsName("ИдтиБыстро"))
			{
				zombie.animator.Play("УдарРазбег", 0, 0f);
			}
			else if (distPlayer.x < 2.5f && stateAnimator.normalizedTime >= 1f && stateAnimator.IsName("УдарРазбег"))
			{
				zombie.animator.Play("Удар", 0, 0f);
			}
			else if (distPlayer.x < 2f && (stateAnimator.normalizedTime >= 1f || stateAnimator.IsTag("stand") || stateAnimator.IsTag("move")))
			{
				zombie.animator.Play("Удар", 0, 0f);
			}
			else if (distPlayer.x <= 3f && stateAnimator.normalizedTime >= 1f && stateAnimator.IsTag("attack"))
			{
				zombie.animator.Play("Удар", 0, 0f);
			}
			else if (distPlayer.x > 3f && strikeCount >= 3 && stateAnimator.normalizedTime >= 0.9f && stateAnimator.IsTag("attack"))
			{
				zombie.SetScaleSpeed(1f);
				zombie.animator.Play("ИдтиБыстро", 0, 0f);
			}
			else if (distPlayer.x >= 3f && stateAnimator.normalizedTime >= 0.9f && stateAnimator.IsTag("attack"))
			{
				zombie.animator.CrossFade("Идти", 0.1f);
				strikeCount++;
			}
			else if (distPlayer.x < 10f && stateAnimator.IsTag("move") && !zombie.IsInvoking("AddScaleSpeed"))
			{
				zombie.Invoke("AddScaleSpeed", 3f);
			}
			if (distPlayer.x < 1.75f)
			{
				if (zombie.speedMove < 0f)
				{
					zombie.transform.position = Player.onStage.transform.position;
					zombie.transform.Translate(1.75f, 0f, 0f);
				}
				else
				{
					Player.onStage.transform.position = zombie.transform.position;
					Player.onStage.transform.Translate(-1.75f, distPlayer.y, 0f);
				}
			}
		}
	}

	public override void OnAttack()
	{
		if (zombie.isDead)
		{
			return;
		}
		if (distBarricade.x <= 0f && ZombieBarricade.onStage != null)
		{
			ZombieBarricade.onStage.OnStrikeZombie(zombie.damageStrike);
		}
		else
		{
			if (!(distPlayer.x <= 3f) || Player.onStage.isDead)
			{
				return;
			}
			Player.onStage.StrikeZombie();
			if (!Player.onStage.isDead)
			{
				return;
			}
			zombie.transform.position = Player.onStage.transform.position;
			zombie.transform.Translate(1.75f, -0.01f, 0f);
			ZombieInHome.UpdateSortingOrder();
			for (int i = 0; i < ZombieInHome.entities.Length; i++)
			{
				if (ZombieInHome.entities[i].isDead)
				{
					continue;
				}
				ZombieInHome.entities[i].CancelInvoke();
				distPlayer = Player.onStage.transform.position - ZombieInHome.entities[i].transform.position;
				distPlayer.x = Mathf.Abs(distPlayer.x);
				stateAnimator = ZombieInHome.entities[i].animator.GetCurrentAnimatorStateInfo(0);
				if (stateAnimator.IsTag("attack"))
				{
					if (distPlayer.x <= 3f)
					{
						ZombieInHome.entities[i].animator.CrossFade("Жрать", 0.1f);
					}
					else
					{
						ZombieInHome.entities[i].animator.CrossFade("Идти", 0.1f);
					}
				}
				else if (distPlayer.x >= 6f && Random.Range(0, 6) != 1)
				{
					ZombieInHome.entities[i].Stand();
				}
			}
		}
	}

	public override void OnDeath()
	{
		for (int i = 0; i < zombie.rigidbodies.Length; i++)
		{
			if (Mathf.Abs(distBarricade.x) <= 1f)
			{
				zombie.rigidbodies[i].AddForce(new Vector2(2f, 0f), ForceMode2D.Impulse);
			}
			else
			{
				zombie.rigidbodies[i].AddForce(new Vector2(zombie.speedMove * 0.5f, 0f), ForceMode2D.Impulse);
			}
		}
	}

	public override bool OnPlayerStrike(string tag)
	{
		Vector3 position = Player.onStage.transform.position;
		float x = position.x;
		Vector3 position2 = zombie.body.position;
		if (Mathf.Abs(x - position2.x) > 3.5f || zombie.isDead || Time.time < timeMarkStrikePlayer || (!string.IsNullOrEmpty(tag) && !zombie.animator.GetCurrentAnimatorStateInfo(0).IsTag(tag)))
		{
			return false;
		}
		stateAnimator = zombie.animator.GetCurrentAnimatorStateInfo(0);
		if (zombie.animator.runtimeAnimatorController == zombie.controllerBreakLegs)
		{
			zombie.animator.Play("ПолучитьУдар", 0, Random.value * 0.1f);
		}
		else if (stateAnimator.IsName("Бег") || stateAnimator.IsName("УдарРазбег"))
		{
			zombie.animator.Play("ПолучитьУдарСтоять", 0, Random.value * 0.1f);
		}
		else if (!zombie.IsStand() || zombie.speedMove == 0f || Random.Range(0, 5) == 1)
		{
			zombie.animator.Play("ПолучитьУдарПрисесть", 0, Random.value * 0.1f);
		}
		else
		{
			zombie.animator.Play("ПолучитьУдар", 0, Random.value * 0.1f);
		}
		zombie.animator.Update(Time.deltaTime);
		timeMarkStrikePlayer = Time.time + stateAnimator.length * 0.5f;
		return true;
	}
}
